﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Common.Serialization;
using ZyGames.Framework.Net;
using ZyGames.Framework.RPC.Sockets;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 好友进入群聊
	/// </summary>
	public class Action30082 : BaseAction
	{
		private int talkingTableId;
		private int userId;
		private bool result = false;

		public Action30082(HttpGet actionGetter)
			: base((short)ActionType.DZEnterTalkingTable, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TalkingTableId", ref talkingTableId) &&
				httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var table = GameTalkingTable.Current.Find(talkingTableId);
			if (table == null)
			{
				Tool.WriteWarmingLine("消息30082群聊组不存在");
				return false;
			}
			var gameUser = GameTable.Current.GetUser(userId);
			if(gameUser == null)
			{
				Tool.WriteWarmingLine("消息30082玩家不存在");
				return false;
			}
			
			if (table.Add(gameUser))
			{
				//广播好友进入群聊
				var userList = table.UsersInTalkingTable;
				var list = new List<IUser>();
				foreach (var user in userList)
				{
					//排除掉当前进入房间的人
					if (user.GetUserId() != gameUser.GetUserId())
						list.Add(new SessionUser(user));
				}
				var parameters = new Parameters();
				parameters["TalkingTableID"] = talkingTableId;
				parameters["UserID"] = userId;
				parameters["NickName"] = gameUser.GetNickName();
				parameters["HeadIcon"] = gameUser.HeadIcon;
				parameters["LoginName"] = gameUser.LoginName;
				ActionFactory.SendAction(list, (int)ActionType.DZBrocastFriendEnterTalkingTable, parameters, (asyncResult) =>
				{
					Console.WriteLine("Action 30083 广播某玩家进入群聊页面 send result:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail");
				});

				list.Clear();
				list.Add(new SessionUser(gameUser));
				var newParams = new Parameters();
				var tableInfo = Convert.ToBase64String(ProtoBufUtils.Serialize(table));
				newParams["TalkingTableData"] = tableInfo;
				newParams["TableMasterName"] = "";
				newParams["TableMasterUserID"] = "";
				//向申请进入房间的人发送其他玩家和房间信息
				ActionFactory.SendAction(list, (int)ActionType.DZNotifyOtherInfoForMe, newParams, (asyncResult) =>
				{
					Console.WriteLine("Action 30084 向申请进入群聊的人发送其他玩家和房间信息 send result:{0}", asyncResult.Result == ResultCode.Success ? "ok" : "fail");
				});
			}
			else
			{
				Tool.WriteErrorLine(string.Format("好友:{0}进入群聊失败", userId));
				return false;
			}
			return true;
		}
	}
}
